package com.suning.sawp.service.impl.dao.mainpushnew;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.intf.dao.mainpushnew.StaffTaskDao;
import com.suning.sawp.po.mainpushnew.StaffModelRel;
import com.suning.sawp.po.mainpushnew.StoreModelRel;
import com.suning.sawp.po.mainpushnew.StoreTask;
import com.suning.sawp.vo.mainpushnew.StaffAssginDetailVo;
import com.suning.sawp.vo.mainpushnew.StaffAssginParam;

@Service
public class StaffTaskDaoImpl implements StaffTaskDao {

	@Resource
	DalClient dalClient;

	@Override
	public List<StaffModelRel> queryStaffModelRels(Long storeTaskId, Long modelId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("storeTaskId", storeTaskId);
		paramMap.put("modelId", modelId);
		List<StaffModelRel> list = dalClient.queryForList("STAFF_MAIN_PUSH.QUERY_STAFF_MODEL_REF", paramMap, StaffModelRel.class);
		return list;
	}

    @Override
    public StoreModelRel queryStoreModelRel(Long storeTaskId, Long modelId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeTaskId", storeTaskId);
        paramMap.put("modelId", modelId);
        StoreModelRel rel = dalClient.queryForObject("STAFF_MAIN_PUSH.QUERY_STORE_MODEL_REL", paramMap, StoreModelRel.class);
        return rel;
    }

    @Override
    public StoreTask queryStoreTaskByStoreTaskId(Long storeTaskId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeTaskId", storeTaskId);
        return dalClient.queryForObject("MAIN_PUSH_CLIENT.QUERY_STORE_TASK_BY_ID", paramMap, StoreTask.class);
    }

    @Override
    @Transactional
    public void assignTask(StaffAssginParam assginParam, StoreTask storeTask) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        // 先删除该款数相关联所有店员任务
        Map<String, Object> delMap = new HashMap<String, Object>();
        delMap.put("storeTaskId", assginParam.getStoreTaskId());
        delMap.put("modelId", assginParam.getModelId());
        dalClient.execute("STAFF_MAIN_PUSH.DEL_STAFF_MODEL_REL", delMap);
        // 新增本次店员款数记录
        Integer distNum = 0;
        for (StaffAssginDetailVo vo : assginParam.getTaskStoreMans()) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("hqTaskId", storeTask.getHqTaskId());
            paramMap.put("storeTaskId", assginParam.getStoreTaskId());
            paramMap.put("modelId", assginParam.getModelId());
            paramMap.put("staffId", vo.getStaffId());
            paramMap.put("staffName", vo.getStaffName());
            paramMap.put("targetNum", vo.getTargetNum());
            mapList.add(paramMap);
            distNum += vo.getTargetNum();
        }
        @SuppressWarnings("unchecked")
        Map<String, Object>[] batchValues = (Map<String, Object>[]) mapList.toArray(new Map[mapList.size()]);
        dalClient.batchUpdate("STAFF_MAIN_PUSH.INSERT_STAFF_MODEL_REL", batchValues);
        // 更新对应的门店款数记录
        StoreModelRel storeModelRel = queryStoreModelRel(assginParam.getStoreTaskId(), assginParam.getModelId());
        Map<String, Object> storeRelMap = new HashMap<String, Object>();
        storeRelMap.put("storeTaskId", assginParam.getStoreTaskId());
        storeRelMap.put("hqTaskId", storeTask.getHqTaskId());
        storeRelMap.put("modelId", assginParam.getModelId());
        storeRelMap.put("targetNum", null != storeModelRel ? storeModelRel.getTargetNum() : null);
        storeRelMap.put("distNum", distNum);
        storeRelMap.put("distStatus", StoreModelRel.DistStatus.HAS_ASSGIN);
        dalClient.execute("STAFF_MAIN_PUSH.REPLACE_STORE_MODEL_REL", storeRelMap);
    }

}
